package com.glorious.gcontacts.spring.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class AddLoginStatusIntercetor extends HandlerInterceptorAdapter
{
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView mav) throws Exception
	{
		if(mav != null)
		{
			UserService service = UserServiceFactory.getUserService();
			User user = service.getCurrentUser();
			mav.addObject("user", user);
			if(user == null)
				mav.addObject("userurl", service.createLoginURL(request.getRequestURL().toString()));
			else
				mav.addObject("userurl", service.createLogoutURL("/"));
		}
	}
}
